/**
 * Copyright (c) 1987-2010 Xiamen BO-tech Communication Software Co., 
 * Ltd. All Rights Reserved.
 * 
 * This software is the confidential and proprietary information of 
 * Xiamen BO-tech Communication Software Co., Ltd. 
 * ("Confidential Information"). You shall not disclose such 
 * Confidential Information and shall use it only in accordance with 
 * the terms of the license agreement you entered into with BO.
 *
 * BO MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
 * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
 * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
 * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. BO SHALL NOT BE LIABLE FOR
 * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
 * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
 */
package com.hns.can.upgrade.upgrade;

import com.hns.can.master.service.WebServiceBSService;
import com.hns.can.upgrade.entity.UpgEqmtVrsnManageLog;
import com.hns.can.upgrade.service.UpgradeFileSetService;
import com.hns.frame.processor.TaskProcessor;
import com.hns.tool.pub.JsonUtil;
import net.sf.json.JSONObject;
import org.springframework.core.task.TaskExecutor;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

/**
 * @版权：HNS 版权所有 (c) 2013
 * @author:zhangzq
 * @version Revision 1.0.0
 * @email:
 * @see:
 * @创建日期：2015-5-5
 * @功能说明：
 * @begin
 * @修改记录:
 * @修改后版本          修改人      	修改内容
 * @2015-5-5  	        zhangzq        	创建
 * @end
 */
public class DevLogUploadProcessor  implements TaskProcessor{	

    // 执行器
    private TaskExecutor        taskExecutor;
    private String commandStr;
    private String logId;
    private UpgradeFileSetService service;
    
    public DevLogUploadProcessor(String logId,String commandStr, TaskExecutor taskExecutor,UpgradeFileSetService service){
    	this.logId=logId;
    	this.commandStr=commandStr;
    	this.taskExecutor=taskExecutor;
    	this.service=service;
    }
	
	/**
	 * {@inheritDoc}
	 * @see TaskProcessor#process()
	 * @author:zhangzq
	 * @email:
	 * @创建日期:2015-5-5
	 * @功能说明：
	 */
	@Override
	public void process() throws Exception {
		 // 使用多线程处理
        taskExecutor.execute(new Runnable() {
            public void run() {
                WebServiceBSService webServiceBSService = new WebServiceBSService();
                Object[] result;
                try {
                	result=webServiceBSService.sendToCommModel(commandStr, "06");
                	 String jsonStr = (result != null ? (String) result[0] : "");
                	 Map jsonMap = JsonUtil.getMapFromJsonString(jsonStr);
                	 JSONObject head = (JSONObject) jsonMap.get("head");
                	 if(head!=null){
                		 String stateCode = head.get("state_code").toString();
                         String stateDesc = head.get("state_desc").toString();
                         if("1".equals(stateCode)&&stateDesc.equals("设备不在线")){
                        	 stateCode="UPST0300";
                        	 stateDesc="UPSM0105";
                        	 //跟新Log
                             UpgEqmtVrsnManageLog logEntity = new UpgEqmtVrsnManageLog();
                             SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                             logEntity.setLogId(logId);
                             logEntity.setUpgradeSt(stateCode);
                             logEntity.setUpgradeEndTime(sdf.format(new Date()));
                             logEntity.setStMsg(stateDesc);
                             service.updateUpgLog(logEntity);
                         }
                	 }
				} catch (Exception e) {
					e.printStackTrace();
				}
            }
        });		
	}

}
